import { getUserInfo } from '@/static/js/user'
import { login } from '@/static/js/login'
import { goEasy } from '@/utils/goEasy.js';
import homeApi from "@/static/js/enterpriseServer/index.js"
export default {
	data() {
		var checkMobile = (rule, value, callback) => {
			if (!value) {
				return callback(new Error('请输入手机号'));
			}
			var reg = /^1[3456789]\d{9}$/;
			if (!reg.test(value)) {
				return callback(new Error('请输入正确的手机号'));
			} else {
				return callback();
			}
		};
		return {
			rules: {
				account: [
					{
						validator: checkMobile,
						trigger: 'blur'
					}
				],
				password: {
					required: true,
					message: '请输入密码',
					trigger: ['blur']
				},
			},
			form: {
				account: '', //	是	string	手机号
				password: "", //		是	string	密码
				app_version: "", //版本号
			},
			checked: false,
			isShow: false,
		};
	},
	methods: {
		isConfirm() {
			this.checked = true;
			this.isShow = false;
			this.signIn()
		},
		//回到上一页
		goBack() {
			uni.navigateBack({
				delta: 1
			});
		},
		onCheck(e) {
			this.checked = !this.checked;
		},

		//登录
		signIn() {
			let that = this;
			if (this.form.account == "") {
				this.$tools.showToast("请输入手机号");
				return false;
			}
			if (this.form.password == "") {
				this.$tools.showToast("请输入密码");
				return false;
			}
			if (this.checked == false) {
				this.isShow = true;
				return false;
			}

			var data = this.form;
			data.number = uni.getDeviceInfo().deviceModel
			data.cid = uni.getStorageSync('cid')
			data.app_version = uni.getStorageSync('version')
			login(data).then(res => {
				if (res.code == 1) {
					this.$tools.showToast("登录成功");
					uni.setStorageSync("token", res.data.token);
					uni.removeStorageSync('lastOpenTime')
					setTimeout(() => {
						goEasy()
					}, 1000);
					// 获取个人信息  存到本地
					getUserInfo().then(res => {
						uni.setStorageSync('userInfo', res.data)
						this.$store.state.userInfo = res.data
						// #ifdef APP-PLUS
						try {

							this.$store.dispatch('createZIM').then((zim_x) => {
								that.getZIM(res.data, that);
							});
						} catch (error) {
							console.log('22222222222222222', error);
						}
						// #endif
					})
					setTimeout(() => {
						uni.switchTab({
							url: "/pages/index/index"
						})
					}, 1000);
				} else {
					this.$tools.showToast(res.msg);
				}
			})
				.catch(err => {
					console.log('错误回调', err);
				});
		},
		jump(url) {
			uni.navigateTo({
				url: url
			})
		},


		getZIM(userInfo, that) {
			// 注册监听“运行时错误信息”的回调
			that.$store.state.zim.on("error", function (zim, errorInfo) {
				console.log("error", errorInfo.code, errorInfo.message);
			});

			// 更新会话列表
			that.$store.state.zim.on("conversationChanged", function (zim, {
				infoList
			}) {
				that.$store.commit('updata_ConversationList', infoList)
			});

			// 注册监听“网络连接状态变更”的回调
			that.$store.state.zim.on(
				"connectionStateChanged",
				function (zim, {
					state,
					event,
					extendedData
				}) {
					// 当长时间网络连接断开导致SDK内部登出时，需要重新登录
					if (state == 0 && event == 3) {
						console.log("网络连接状态变更");
						that.$store.dispatch('ZIMLogin')
					}
				}
			);

			// 注册监听“令牌即将过期”的回调
			that.$store.state.zim.on("tokenWillExpire", function (zim, {
				second
			}) {
				// console.log("令牌即将过期", second);
				// 可以在这里调用 renewToken 接口来更新 token
				// 新 token 生成可以参考上文
				let token = uni.getStorageSync("zegoToken");
				uni.removeStorageSync("zegoToken");
				zim.renewToken(token)
					.then(function ({
						token
					}) {
						// 更新成功
						uni.setStorageSync("zegoToken", token);
						that.$store.dispatch('ZIMLogin')
					})
					.catch(function (err) {
						// 更新失败
						that.$store.dispatch('ZIMLogin')
					});
			});


			that.$store.dispatch('ZIMLogin')
		},


		//请求企服过滤数据数据
		filtration() {
			homeApi.filtration().then(res => {
				let arr = res.data;
				let userIds = arr.map(item => item.user_id);
				//存到vuex中
				this.$store.commit('set_filtration', userIds)
			})
		},
	},
	onLoad(e) {
		console.log(e, 'onLoad');
		if (e.tel) {
			this.form.account = e.tel;
		}
		this.filtration()
	},
	onLaunch() {
	},
	onShow() {

	},
};